//
//  KINGVisitorView.swift
//  肉荣微博
//
//  Created by king on 15/7/29.
//  Copyright © 2015年 king. All rights reserved.
//

import UIKit
protocol KINGVisitorViewDelegate : NSObjectProtocol {
    func registerBtnDidClick ();
    func loginBtnDidClick ();
}

class KINGVisitorView: UIView {
    //代理属性一定要定义成weak
    weak var delegate : KINGVisitorViewDelegate?
    //设置登录视图显示的内容，每个子视图都不一样，需要调用这个方法进行设置
    func setupViewInfo(isHome:Bool, imageName:String, message:String) {
        messageLabel.text = message
        homeIconView.image = UIImage(named: imageName)
        iconView.hidden = !isHome
        isHome ? startAnimation() : sendSubviewToBack(maskIconView)
    }
    //设置头像的动画
    private func startAnimation() {
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.toValue = M_PI * 2
        anim.repeatCount = MAXFLOAT
        anim.duration = 20.0
        //这里一定要注意，可以解决切换回来之后，动画还存在
        anim.removedOnCompletion = false
        iconView.layer.addAnimation(anim, forKey: nil)
    }

    override init(frame: CGRect) {
        //一定要调用父类的init方法
        super.init(frame: frame)
        backgroundColor = UIColor(white: 237.0/255.0, alpha: 1.0)
        setupUI()
        startAnimation()
    }
    required init?(coder aDecoder: NSCoder) {
        //这个方法在纯代码创建的时候不会调用，有下面这句代码是为了防止sb中使用自定义视图，如果使用直接报错
        //如果不想报错，直接注释掉，使用父类的方法即可
        //fatalError("init(coder:) has not been implemented")
        super.init(coder: aDecoder)
    }
/// 设置UI
    private func setupUI() {
        addSubview(iconView)
        addSubview(homeIconView)
        addSubview(maskIconView)
        addSubview(messageLabel)
        addSubview(registerBtn)
        addSubview(loginBtn)
        //print(self.center)
        //圆环视图设置约束
        iconView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem:self , attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem:self , attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 0))
        //主头像控件设置约束
        homeIconView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: homeIconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem:iconView , attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: homeIconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem:iconView , attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 0))
        //显示信息控件设置约束
        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 240))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 34))
        //注册按钮设置约束
        registerBtn.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Left, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 35))
        //登录按钮设置约束
        loginBtn.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Right, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 35))
        //遮罩视图设置约束
        maskIconView.translatesAutoresizingMaskIntoConstraints = false
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[mask]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["mask":maskIconView]))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[mask]-(-70)-[message]", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["mask":maskIconView, "message":messageLabel]))
        
    }
    
    //设置监听方法，时间被触发，调用代理的方法
    func clickRegisterBtn () {
        delegate?.registerBtnDidClick()
    }
    
    func clickLoginBtn() {
        delegate?.loginBtnDidClick()
    }
    /// 懒加载注册按钮
    private lazy var registerBtn: UIButton = {
        let regBtn = UIButton()
        regBtn.setTitle("注册", forState: UIControlState.Normal)
        regBtn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        regBtn.setBackgroundImage(UIImage(named:"common_button_white_disable"), forState: UIControlState.Normal)
        regBtn.sizeToFit()
        regBtn.addTarget(self, action: "clickRegisterBtn", forControlEvents: UIControlEvents.TouchUpInside)
        return regBtn
    }()
    /// 登录按钮
    private lazy var loginBtn: UIButton = {
        let lgnBtn = UIButton()
        lgnBtn.setTitle("登录", forState: UIControlState.Normal)
        lgnBtn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        lgnBtn.setBackgroundImage(UIImage(named:"common_button_white_disable"), forState: UIControlState.Normal)
        lgnBtn.sizeToFit()
        lgnBtn.addTarget(self, action: "clickLoginBtn", forControlEvents: UIControlEvents.TouchUpInside)
        return lgnBtn
    }()
    /// 遮罩图片视图
    private lazy var maskIconView: UIImageView = {
        let mask = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
        return mask
    }()
    /// 圆环图片控件
    private lazy var iconView: UIImageView = {
        let icon = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
        return icon
    }()
    /// 主图片控件
    private lazy var homeIconView: UIImageView = {
        let home = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
        return home
    }()
    /// 显示信息label
    private lazy var messageLabel: UILabel = {
        let lb = UILabel()
        lb.text = "我就是那宇宙无敌的超级英雄猪肉荣"
        lb.textColor = UIColor.darkGrayColor()
        lb.font = UIFont.systemFontOfSize(14.0)
        lb.numberOfLines = 0
        lb.textAlignment = NSTextAlignment.Center
        lb.sizeToFit()
        return lb
    }()
}
